﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ServBus.ListForm2010.Utility;
using ServBus.ListForm2010.Resources;

namespace ServBus.ListForm2010.Layouts.InfoBus2010
{
    public partial class LookupFormManagement : LayoutsPageBase
    {
        #region Global Variable

        private SPList mList;
        private SPWeb mWeb;
        private SPSite mSite;
        private static DllResource Resource = new DllResource("ServBus.ListForm2010.Common.Resources|AppPages");

        #endregion

        #region Control Event

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    InitialPage();
                }
            }
            catch (Exception ex)
            {
                LTException.Text = ex.ToString();
            }
        }

        protected void createlkpFormButtonClick(object sender, EventArgs e)
        {
            string redirectUrl = SPContext.Current.Web.Url + "/_layouts/InfoBus2010/LookupFormGenerator.aspx?source=" + Request["source"];
            Response.Redirect(redirectUrl);
        }

        protected void openButtonClick(object sender, EventArgs e)
        { }

        protected void deleteButtonClick(object sender, EventArgs e)
        { }

        protected void cancelButtonClick(object sender, EventArgs e)
        {
            Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('cancel');</script>");
        }

        #endregion

        #region Private Method

        private bool InitialPage()
        {
            //Initial Parameters
            bool initialStatus = false;
            if (!string.IsNullOrEmpty(Request["source"]))
            {
                initialStatus = true;
                Guid _siteId = SPContext.Current.Site.ID;
                Guid _webId = SPContext.Current.Web.ID;
                Guid _listId = new Guid(Request["source"]);
                //Set lkpFormbl default status
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite _site = new SPSite(_siteId))
                    {
                        using (SPWeb _web = _site.OpenWeb(_webId))
                        {
                            SPList _list = _web.Lists[_listId];
                            SPList _lkpFormConfigList = _web.Lists[Constants.LookupForm_Config_NAME];

                            //Retrieve lookup form relation of source list
                            SPQuery _lkpFormConfigQuery = new SPQuery();
                            _lkpFormConfigQuery.Query = "<Where><Eq><FieldRef Name='Title'/>" +
                                                        "<Value Type='Text'>" + _list.Title + "</Value></Eq></Where>";

                            SPListItemCollection resultItems = _lkpFormConfigList.GetItems(_lkpFormConfigQuery);
                            foreach (SPListItem rItem in resultItems)
                            {
                                string sTitle = rItem.Title;
                                string sValue = Convert.ToString(rItem["ConfigType"]);

                                lkpFormbl.Items.Add(new ListItem(sTitle + " - " + sValue, sTitle + ";" + sValue));
                            }
                        }
                    }
                });
            }
            return initialStatus;
        }

        #endregion
    }
}
